//
//  TopicDetailController.m
//  Qzone
//
//  Created by zhangxy on 2018/7/4.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "TopicDetailController.h"


#import <SDWebImage/UIImageView+WebCache.h>
#import "UserInfoController.h"
#import "ZZProgressVIew.h"
#import "ZZShareView.h"

@interface TopicDetailController (){
    int Space;
    
}

@property(nonatomic,strong)  UIScrollView *contentScroll;

@property(nonatomic,strong)  UIImageView *imgHeader;
@property(nonatomic,strong)  UILabel *labTitle;
@property(nonatomic,strong)  UILabel *labTime;
@property(nonatomic,strong)  UILabel *labDesc;
@property(nonatomic,strong)  UIView *imgContent;
@property(nonatomic,strong)  UILabel *labType;
@property(nonatomic,strong)  UILabel *labDepartment;
@property(nonatomic,strong)  UILabel *labName;
@property(nonatomic,strong)  UIImageView *imgAvatar;
@property(nonatomic,strong)  ZZProgressVIew *progressGoodView;
@property(nonatomic,strong)  ZZProgressVIew *progressBadView;
@property(nonatomic,strong)  UIButton *btnShare;

@end

@implementation TopicDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [super viewDidLoad];
    self.navigationItem.title = @"专题";
    [self setNavigationBar];
    
    self.navigationController.navigationBarHidden = NO;
    
    Space = 30;
    
    
    [self.view addSubview:self.contentScroll];
    [self addContentSubViews];
    
    [self loadMoreData];
    [self setDataToView];
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}


-(void)setDataToView{
    if(_model){
        [_imgHeader sd_setImageWithURL:[NSURL URLWithString:_model.image_url]];
        [_labTitle setText:convertToString(_model.title)];
        [_labTime setText:convertToString(_model.add_time)];
    }
}

-(void)OnClickAddvote:(NSString  *) type{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:_model.t_id forKey:@"id"];
    [params setObject:convertToString([UserUtil getUser].user_id) forKey:@"session_uid"];
    [params setObject:convertToString(type) forKey:@"type"];
    
    [NetworkUtil POST:API_GetSpecialAddvote parameters:params success:^(id responseObject, NSDictionary *dict) {
        
        [PopUtil showMessage:convertToString(dict[@"msg"])];
        CGFloat f = 0;
        if([@"like" isEqual:type]){
            self.model.like_count = dict[@"data"][@"count"];
            f = [self.model.like_count floatValue]/[self.model.view_count floatValue];
            if(f>1){
                f = 1;
            }
            
            [self.progressGoodView changeValue:@{@"value":[NSString stringWithFormat:@"%0.2f",f],@"title":@"加鸡腿",@"icon":@"icon_praise_personalpage"}];
        }else{
            self.model.olike_count = dict[@"data"][@"count"];
            f = [self.model.like_count floatValue]/[self.model.view_count floatValue];
            if(f>1){
                f = 1;
            }
         
            [self.progressBadView changeValue:@{@"value":[NSString stringWithFormat:@"%0.2f",f],@"title":@"加鸡腿",@"icon":@"icon_praise_personalpage"}];
        }
    } failure:^(NSError *error, NSString *message) {
        [PopUtil showMessage:convertToString(message)];
    }];
}


/**
 加载更多
 */
-(void)loadMoreData{
    NSString *api = API_GetSpeciaDetail;
    
    NSDictionary *p = @{@"id":_model.t_id,@"page":@"1"};
    
    [NetworkUtil POST:api parameters:p success:^(id responseObject, NSDictionary *dictionary) {
        
        
        NSArray *arr = nil;
        if([dictionary[@"data"] isKindOfClass:[NSDictionary class]]){
            arr = dictionary[@"data"][@"list"];
        }else{
            arr = dictionary[@"data"][0][@"list"];
        }
        
        
        
    } failure:^(NSError *error, NSString *message) {
        [PopUtil showMessage:message];
    }];
}


- (UIScrollView *)contentScroll
{
    if (_contentScroll == nil) {
        _contentScroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _contentScroll.backgroundColor = [UIColor clearColor];
        _contentScroll.showsHorizontalScrollIndicator = NO;
        _contentScroll.showsHorizontalScrollIndicator = NO;
        _contentScroll.showsVerticalScrollIndicator = NO;
        _contentScroll.alwaysBounceHorizontal = NO;
        _contentScroll.alwaysBounceVertical = YES;
        _contentScroll.pagingEnabled = NO;
        //    _mainScroll.bounces = NO;
        _contentScroll.scrollEnabled = YES;
       
    }
    return _contentScroll;
}


-(void)addContentSubViews{
    _imgHeader = [self createImageView];
    [_imgHeader setFrame:CGRectMake(0, 0, ScreenWidth, 200)];
    
    _labTitle = [self createLabel];
    [_labTitle setFont:FontSeventeen];
    [_labTitle setTextColor:UIColorFromRGB(TextSizeOneColor)];
    [_labTitle setFrame:CGRectMake(20, CGRectGetMaxY(_imgHeader.frame) + 20, ScreenWidth - 140, 24)];
    
    _labTime = [self createLabel];
    [_labTime setFont:FontThirteen];
    [_labTime setTextColor:UIColorFromRGB(TextSizeEightColor)];
    [_labTime setTextAlignment:NSTextAlignmentRight];
    [_labTime setFrame:CGRectMake(ScreenWidth-120, CGRectGetMaxY(_imgHeader.frame) + 20, 100, 24)];
    
    _labDesc = [self createLabel];
    [_labDesc setFont:FontFiftent];
    [_labDesc setTextColor:UIColorFromRGB(TextSizeOneColor)];
    [_labDesc setFrame:CGRectMake(20, CGRectGetMaxY(_labTitle.frame) + Space, ScreenWidth - 140, 24)];
    
    
    _imgContent = [[UIView alloc] init];
    [_imgContent setBackgroundColor:UIColor.clearColor];
    [_imgContent setFrame:CGRectMake(20, CGRectGetMaxY(_labDesc.frame) + Space, ScreenWidth - 40, 0)];
    
    _labType = [self createLabel];
    [_labType setFont:FontThirteen];
    [_labType setTextColor:UIColorFromRGB(TextSizeEightColor)];
    [_labType setFrame:CGRectMake(20, CGRectGetMaxY(_imgContent.frame) + Space, ScreenWidth - 40, 18)];
    
    
    _labDepartment = [self createLabel];
    [_labDepartment setFont:FontThirteen];
    [_labDepartment setTextColor:UIColorFromRGB(Text6Color)];
    [_labDepartment setFrame:CGRectMake(20, CGRectGetMaxY(_labType.frame) + Space, ScreenWidth - 40, 18)];
    
    
    _imgAvatar = [self createImageView];
    [_imgAvatar setFrame:CGRectMake(20, CGRectGetMaxY(_labDepartment.frame) + Space, 44, 44)];
    _imgAvatar.layer.cornerRadius = 22.0f;
    _imgAvatar.layer.masksToBounds = YES;
    
    _labName = [self createLabel];
    [_labName setFont:FontSeventeen];
    [_labName setTextColor:UIColorFromRGB(TextSizeOneColor)];
    [_labName setFrame:CGRectMake(20 + 44 + 10, CGRectGetMaxY(_labDepartment.frame) + Space, ScreenWidth - 94, 44)];
    
    
    _progressGoodView = [[ZZProgressVIew alloc] initWithBgColor:UIColorFromRGB(0xFFADCC) tintColor:UIColorFromRGB(0xFF70A6) frame:CGRectMake(74, CGRectGetMaxY(_labName.frame) + 15, ScreenWidth - 94, 44) value:@{@"value":@"0",@"title":@"加鸡腿",@"icon":@"icon_praise_personalpage"}];
    [_progressGoodView setOnItemClickBlock:^(ZZProgressViewType clickType, NSString *title) {
        [self OnClickAddvote:@"like"];
    }];
    
    _progressBadView = [[ZZProgressVIew alloc] initWithBgColor:UIColorFromRGB(0xFFADCC) tintColor:UIColorFromRGB(0xFF70A6) frame:CGRectMake(74, CGRectGetMaxY(_progressGoodView.frame) + 15, ScreenWidth - 94, 44) value:@{@"value":@"0",@"title":@"扣工资",@"icon":@"icon_praise_personalpage"}];
    [_progressBadView setOnItemClickBlock:^(ZZProgressViewType clickType, NSString *title) {
        [self OnClickAddvote:@"drop"];
    }];
    
    
    _btnShare = [self createButton];
    [_btnShare setFrame:CGRectMake(ScreenWidth - 64, CGRectGetMaxY(_progressBadView.frame) + 20, 44, 44)];
    [_btnShare setImage:[UIImage imageNamed:@"btn_share_weibo"] forState:0];
    [_btnShare addTarget:self action:@selector(tapClick:) forControlEvents:UIControlEventTouchUpInside];
    [_btnShare setBackgroundColor:UIColor.clearColor];
    
    
    [_contentScroll addSubview:_imgHeader];
    [_contentScroll addSubview:_labTitle];
    [_contentScroll addSubview:_labTime];
    [_contentScroll addSubview:_labDesc];
    [_contentScroll addSubview:_imgContent];
    [_contentScroll addSubview:_labType];
    [_contentScroll addSubview:_labDepartment];
    [_contentScroll addSubview:_imgAvatar];
    [_contentScroll addSubview:_labName];
    [_contentScroll addSubview:_progressGoodView];
    [_contentScroll addSubview:_progressBadView];
    [_contentScroll addSubview:_btnShare];
}

-(void)setItemsFrame{
    [_labTitle setFrame:CGRectMake(20, CGRectGetMaxY(_imgHeader.frame) + 20, ScreenWidth - 140, 24)];
    [_labTime setFrame:CGRectMake(ScreenWidth-120, CGRectGetMaxY(_imgHeader.frame) + 20, 100, 24)];
    [_labDesc setFrame:CGRectMake(20, CGRectGetMaxY(_labTitle.frame) + Space, ScreenWidth - 140, 24)];
    [_imgContent setFrame:CGRectMake(20, CGRectGetMaxY(_labDesc.frame) + Space, ScreenWidth - 40, 0)];
    [_labType setFrame:CGRectMake(20, CGRectGetMaxY(_imgContent.frame) + Space, ScreenWidth - 40, 18)];
    [_labDepartment setFrame:CGRectMake(20, CGRectGetMaxY(_labType.frame) + Space, ScreenWidth - 40, 18)];
    [_imgAvatar setFrame:CGRectMake(20, CGRectGetMaxY(_labDepartment.frame) + Space, 44, 44)];
    [_labName setFrame:CGRectMake(20 + 44 + 10, CGRectGetMaxY(_labDepartment.frame) + Space, ScreenWidth - 94, 44)];
    [_progressGoodView setFrame:CGRectMake(74, CGRectGetMaxY(_labName.frame) + 15, ScreenWidth - 94, 44)];
    [_progressBadView setFrame:CGRectMake(74, CGRectGetMaxY(_progressGoodView.frame) + 15, ScreenWidth - 94, 44)];
    [_btnShare setFrame:CGRectMake(ScreenWidth - 64, CGRectGetMaxY(_progressBadView.frame) + 20, 44, 44)];
    
    
    [_contentScroll setContentSize:CGSizeMake(ScreenWidth, CGRectGetMaxY(_btnShare.frame)+20)];
}

-(UIImageView *)createImageView{
    UIImageView *imgView = [[UIImageView alloc] init];
    [imgView setContentMode:UIViewContentModeScaleAspectFill];
    imgView.clipsToBounds = YES;
    return imgView;
}

-(UILabel *) createLabel{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    [label setTextAlignment:NSTextAlignmentLeft];
    [label setFont:FontThirteen];
    [label setTextColor:UIColorFromRGB(TextSizeOneColor)];
    [label setBackgroundColor:UIColor.clearColor];
    [label setNumberOfLines:0];
    return label;
}

-(UIButton *) createButton{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn.titleLabel setFont:FontFiftent];
    return btn;
}





-(void)tapClick:(UIButton *) tap{
    ZZShareView *share = [[ZZShareView alloc] initWithShareType:ZZShareTypeTopic vc:self obj:_model];
    [share show];
    
}



#pragma mark UITableView delegate end
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
